// pages/Personals/TrendsDetail/TrendsDetail.js
//获取应用实例
const app = getApp()
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { queryPetTrendsInfo , updatePetTrendsCommentReply } from '../../../api/pet';
import { checkFile } from '../../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    assetsAddress: assetsAddress,
    CustomBar: app.globalData.CustomBar,
    item: {

    },
    commentCont: '',
    showFieldModal: false,
    commentId: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.item){
      this.setData({
        item: JSON.parse(options.item)
      })
      console.log(this.data.item,'trends')
      this._queryPetTrendsInfo();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    let userId = wx.getStorageSync('userId') ?? 10;
    return {
      path: '/pages/Welcome/Welcome?scene='+userId,
    }
  },
  // 展示评论输入框
  showCommentIpt(e){
    this.setData({
      showFieldModal: true,
      commentCont: '',
      commentId: '',
    })
  },
  // 单独回复某条评论
  showCommentId(e){
    let commentId = e.target.dataset.commentid||e.currentTarget.dataset.commentid;
    this.setData({
      showFieldModal: true,
      commentCont: '',
      commentId: commentId,
    })
  },
  // 输入框失去焦点隐藏输入框
  commentNBlur(){
    this.setData({
      showFieldModal: false,
    })
  },
  commentContChange(e){
    this.setData({
      commentCont: e.detail,
    })
  },
  // 回复评论内容
  commentSubmit(){
    let _this = this;
    if(_this.data.commentCont == ''){
      return
    }else{
      updatePetTrendsCommentReply({trendsId: _this.data.item.id,content: _this.data.commentCont,commentId: _this.data.commentId})
      .then(async res => {
        if(res.success){
          wx.showToast({
            title: _this.data.commentId != '' ? '回复成功' : '评论成功',
            icon: 'success',
          })
          _this._queryPetTrendsInfo();
          const eventChannel = _this.getOpenerEventChannel()
          eventChannel.emit('updateTrends',_this.data.item.id);
        }else{
          wx.showToast({
            title: res.errMessage,
            icon: 'none',
          })
        }
      })
    }
  },
  _queryPetTrendsInfo(){
    let _this = this;
    queryPetTrendsInfo({id: _this.data.item.id,isNeedUserId: '0',userId: wx.getStorageSync('userId')})
    .then(res => {
      if(res.success){
        if(res.data.content.length > 0){
          var item = res.data.content[0];
          if(item.fileUrl && item.fileUrl != 'null' && item.fileUrl != ''){
            item.imageUrl = item.fileUrl.split(',')
            var fileList = []
            for (let j = 0; j < item.imageUrl.length; j++) {
              const img = item.imageUrl[j];
              var types = checkFile(img);
              if(types == 'video'){
                fileList.push({url: img,isVideo: true,deletable: true})
              }else if(types == 'image'){
                fileList.push({url: img,isImage: true,deletable: true})
              }
            }
            item.imageUrl = fileList;
          }else{
            item.imageUrl = []
          }
          var commentList = [];
          for (let index = 0; index < item.petTrendsCommentReplyDTOList.length; index++) {
            const itm = item.petTrendsCommentReplyDTOList[index];
            itm.children = [];
            if(itm.replyId == null){
              commentList.push(itm);
            }else{
              for (let i = 0; i < commentList.length; i++) {
                const element = commentList[i];
                // console.log(element,'element',itm)
                if(element.id == itm.replyId){
                  commentList[i].children.push(itm);
                }
              }
            }
          }
          item.commentList = commentList;
          console.log(item,'item');
          _this.setData({
            item: item,
          })
        }
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 查看图片
  goLink(e){
    let url = e.target.dataset.url||e.currentTarget.dataset.url;
    let item = e.target.dataset.item||e.currentTarget.dataset.item;
    wx.navigateTo({
      url: `/pages/Link/LinkImg/LinkImg?url=${encodeURIComponent(url)}&title=${JSON.stringify(item.content)}`,
    })
  },
  // 查看视频
  goVideo(e){
    let url = e.target.dataset.url||e.currentTarget.dataset.url;
    let item = e.target.dataset.item||e.currentTarget.dataset.item;
    wx.navigateTo({
      url: `/pages/Link/LinkVideo/LinkVideo?url=${encodeURIComponent(url)}&title=${JSON.stringify(item.content)}`,
    })
  },
})